﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KeZhou.Views.Components.Customs
{
    /// <summary>
    /// RatingControl.xaml 的交互逻辑
    /// </summary>
    public partial class RatingControl : UserControl
    {
        public static readonly DependencyProperty RateProperty = DependencyProperty.Register("Rate", typeof(double), typeof(RatingControl));

        public double Rate { get { return (double)GetValue(RateProperty); } set { SetValue(RateProperty, value); } }

        public static readonly DependencyProperty StarClorProperty = DependencyProperty.Register("StarColor", typeof(string), typeof(RatingControl));

        public string StarColor { get { return (string)GetValue(StarClorProperty); } set { SetValue(StarClorProperty, value); } }

        public RatingControl()
        {
            InitializeComponent();
        }

        private void setOffset()
        {
            List<double> offsets = new List<double> { 0, 0, 0, 0, 0 };
            for (int i = 0; i < 5; i++)
            {
                if (Rate >= i + 1)
                {
                    offsets[i] = 1;
                }
                else
                {
                    offsets[i] = Rate - i;
                    break;
                }
            }
            star0_offset1.Offset = offsets[0];
            star0_offset2.Offset = offsets[0];
            star1_offset1.Offset = offsets[1];
            star1_offset2.Offset = offsets[1];
            star2_offset1.Offset = offsets[2];
            star2_offset2.Offset = offsets[2];
            star3_offset1.Offset = offsets[3];
            star3_offset2.Offset = offsets[3];
            star4_offset1.Offset = offsets[4];
            star4_offset2.Offset = offsets[4];
        }

        private void setColor()
        {
            star0.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(StarColor));
            star1.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(StarColor));
            star2.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(StarColor));
            star3.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(StarColor));
            star4.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(StarColor));
        }

        private void RateControl_Loaded(object sender, RoutedEventArgs e)
        {
            setColor();
            setOffset();
        }

    }
}
